# -*- coding:utf-8 -*-
import sys
from PySide6.QtWidgets import (QMainWindow, QWidget, QVBoxLayout)

from app.view.widget.excel_group import ExcelGroup
from app.view.widget.export_group import ExportGroup
from app.view.widget.delimiter_group import DelimiterGroup
from app.view.widget.export_type_group import ExportTypeGroup
from app.view.widget.btn_group import BtnGroup
from app.view.widget.log_group import LogGroup

class AppView(QMainWindow):
    """
    mvc 框架 view
    """
    def __init__(self):
        super().__init__()
        self.controller = None
        self.init_ui()
        self.setWindowTitle("Excel2xxx excel转换格式小工具 （裴文浩 64054443@qq.com|刘晓 769626392@qq.com)")
        self.resize(800, 600)

    def init_ui(self):
        central_widget = QWidget()
        self.setCentralWidget(central_widget)
        # 整体布局（垂直层
        main_layout = QVBoxLayout(central_widget)
        # 上半部分：文件浏览器和设置区域 (垂直)
        top_group = QVBoxLayout()        
        # 1. 读取Excel路径设置
        self.excel_group = ExcelGroup()
        top_group.addWidget(self.excel_group)
        # 2. 导出Export路径设置
        self.export_group = ExportGroup()
        top_group.addWidget(self.export_group)
        # 3. 中英文分割符
        self.delimiter_group = DelimiterGroup()
        top_group.addWidget(self.delimiter_group)
        # 4. 导出类型选择
        self.export_type_group = ExportTypeGroup()
        top_group.addWidget(self.export_type_group)
        # 5. 新增操作按钮组
        self.btn_group = BtnGroup()
        top_group.addWidget(self.btn_group)

        top_group.addLayout(top_group, 1) 
        main_layout.addLayout(top_group)
        # 下半部分：新增日志显示区
        self.log_group = LogGroup()
        main_layout.addWidget(self.log_group)

    def bind_controller(self, controller):
        self.controller = controller
        # 绑定
        self.excel_group.excel_btn_choose.clicked.connect(controller.handle_excel_directory_selection)
        self.export_group.export_btn_choose.clicked.connect(controller.handle_export_directory_selection)
        self.delimiter_group.delimiter_edit.editingFinished.connect(controller.handle_delimiter_editing_finished)
        for cb in self.export_type_group.export_type_cb:
            cb.stateChanged.connect(controller.handle_export_type_cb_update)
        # 新增绑定
        self.btn_group.btn_clear_log.clicked.connect(controller.handle_clear_log)
        self.btn_group.btn_refresh.clicked.connect(controller.handle_refresh)
        self.btn_group.btn_export.clicked.connect(controller.handle_export)
        

    # 视图更新方法
    def update_excel_path(self, path):
        self.excel_group.excel_path_input.setText(path)

    def update_export_path(self, path):
        self.export_group.export_path_input.setText(path)

    def update_feature_status(self, enabled):
        self.enable_check.setChecked(enabled)
        self.btn_group.btn_export.setEnabled(enabled)

    def update_log_view(self, content):
        self.log_group.log_view.setPlainText(content)

